<?php
/**
 * Calendar page controller
 *
 * @author Dima
 *
 */

class CalendarController extends modules_default_controllers_ControllerBase
{
	
//---------------------------------------------------------------------------------------------------------------------------
	
	protected $_bForLoggedUsersOnly = true;
	protected $_contentLayout = 'contentonly';
		
//---------------------------------------------------------------------------------------------------------------------------
		
	public $ajaxable = array(
        'save' => array('json'),
        'delete' => array('json'),
        'index' => true,
    );
    
//---------------------------------------------------------------------------------------------------------------------------
        
	public function init()
	{
//		$aRights = Lemar_Rights::getUserRights();
//		$this->_bEditable 	  = $aRights['calendar'];
//		$this->view->editable = $this->_bEditable;
		
		if (! $this->getRequest()->isXmlHttpRequest())
        {
    		parent::init();
            $this->view->title = 'Calendar';

            $this->view->headLink()->appendStylesheet('/css/calendar.css');
            $this->view->headLink()->appendStylesheet('/css/ui-lightness/jquery-ui.css');
            $this->view->headLink()->appendStylesheet('/css/jquery.alerts.css');
            
            $this->view->headScript()->appendFile('/js/lib/jquery-ui.js');
            $this->view->headScript()->appendFile('/js/lib/jquery.alerts.js');
            $this->view->headScript()->appendFile('/js/calendar.js');
			$this->view->headScript()->appendFile('/js/resources.js');
        }

        $this->_helper->ajaxContext()->initContext('json');
	}
	
//---------------------------------------------------------------------------------------------------------------------------
		
	/**
	 * load calendar data
	 *
	 */
	public function indexAction()
	{
		$oRequest 	 = $this->getRequest();
		$iYear		 = $oRequest->getParam('year', 0);
		$iMonth		 = $oRequest->getParam('month', 0);
		$iYear  	 = (int) (($iYear !== 0) ? $iYear : date('Y'));
		$iMonth 	 = (int) (($iMonth !== 0) ? $iMonth : date('m'));
		
		$iFirstDayTS = date('Y-m-d', mktime(0, 0, 0, $iMonth, 1, $iYear));
		$iLastDayTS  = date('Y-m-d', mktime(0, 0, 0, $iMonth+1, 1, $iYear));
		
		
        $aCalendarData 			= Lemar_Calendar::indexCalendar($this->getRequest());
        $allEvent				= models_EventMapper::findByIdUserAndDate(Lemar_User::getLoggedUserId(), $iFirstDayTS, $iLastDayTS); 
		$allAddressBook			= models_AddressMapper::getAll(1, 99999);
        
        $this->view->allAddressBook	= $allAddressBook['array'];
        $this->view->allEvent 		= $allEvent;
        $this->view->aYears 		= Lemar_Calendar::getYears();
        $this->view->iDPW 			= Lemar_Calendar::DAYS_PER_WEEK;
        $this->view->sTop 			= $aCalendarData['top'];
        $this->view->iYear 			= $aCalendarData['year'];
        $this->view->iMonth 		= $aCalendarData['month'];
        $this->view->aDayNames 		= $aCalendarData['dayNames'];
        $this->view->aWeek 			= $aCalendarData['weeks'];
	}
	
//---------------------------------------------------------------------------------------------------------------------------
	
	public function addAction()
	{
//		phpinfo();
		$oRequest 	 = $this->getRequest();
		if ($oRequest->isPost())
		{
			$for		 	= $oRequest->getParam('for');
			$description 	= $oRequest->getParam('description');
			$date_m		 	= $oRequest->getParam('date_m'); 
			if(2 > strlen($date_m))
			{
				$date_m = 0 . $date_m;
			}
			$date_d		 	= $oRequest->getParam('date_d'); 
			$date_y		 	= $oRequest->getParam('date_y');
			$remind_before	= $oRequest->getParam('remind_before');

			if (3 > strlen($date_y))
			{
				unset($date_y);
			}
			$aData			= array('date_md' 			=> $date_m . '-' . $date_d,
									'days_send_before'	=> $remind_before,
									'in_cycle'			=> (isset($date_y) ? 0 : 1),
									'id_user'			=> Lemar_User::getLoggedUserId(),
									'description'		=> $description,
									'date_y'			=> $date_y,
			
									);
									print_r($aData);
			models_EventMapper::saveArray($aData, 'models_DbTable_Event');
			$this->_redirect('/calendar');
		}	
	}
	
//---------------------------------------------------------------------------------------------------------------------------
        
    public function editAction()
    {
		$oRequest 	= $this->getRequest();
		$id			= $oRequest->getParam('id');
		if ($oRequest->isPost())
		{
			$for		 	= $oRequest->getParam('for');
			$description 	= $oRequest->getParam('description');
			$date_m		 	= $oRequest->getParam('date_m'); 
			if(2 > strlen($date_m))
			{
				$date_m = 0 . $date_m;
			}
			$date_d		 	= $oRequest->getParam('date_d'); 
			$date_y		 	= $oRequest->getParam('date_y');
			$remind_before	= $oRequest->getParam('remind_before');

			if (3 > strlen($date_y))
			{
				unset($date_y);
			}
			$aData			= array('date_md' 			=> $date_m . '-' . $date_d,
									'days_send_before'	=> $remind_before,
									'in_cycle'			=> (isset($date_y) ? 0 : 1),
									'id_user'			=> Lemar_User::getLoggedUserId(),
									'description'		=> $description,
									'date_y'			=> $date_y,
			
									);
									print_r($aData);
			models_EventMapper::update($id, $aData, 'models_DbTable_Event');
			$this->_redirect('/calendar');
		}
		
		$item 					= models_EventMapper::findById($id);
		$allAddressBook			= models_AddressMapper::getAll(1, 99999);
        
        $this->view->allAddressBook	= $allAddressBook['array'];
		$this->view->item			= $item[0];
    }
    
//---------------------------------------------------------------------------------------------------------------------------
        
    /**
     * @author Dima
     * delete appointment from calendar
     */
    public function deleteAction()
    {
		$oRequest 	= $this->getRequest();
		$id		= $oRequest->getParam('id');
		
		if (models_EventMapper::deleteFromBase($id, 'models_DbTable_Event'))
		{
			$this->view->ret = 1;
		}
		else 
		{
			$this->view->ret = 0;
		}
    }
    
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
}